<?php

require_once "MyRabbitMq.php";
use MyRabbitMq\MyRabbitMq;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use \PhpAmqpLib\Wire\AMQPTable;


$queueName = "Queue1"; //生产消息的队列
$exchangeName = "Exchange1";//生产消息的队列绑定的交换机

$Ttl = 1000*10; //10s过期


$rabbit = new MyRabbitMq();
$ch = $rabbit->getChannel();


$rabbit->setConfirm();

$rabbit->setAckHandler(function (AMQPMessage $message){
    echo "传递成功!".$message->getBody().PHP_EOL;
});

$rabbit->setNAckHandler(function (AMQPMessage $message){
    echo "传递失败!".$message->getBody().PHP_EOL;
});


$data = [
    'x-message-ttl' => $Ttl, //消息存活时间
];
$arguments = new AMQPTable($data);
//普通队列绑定定时过期
$rabbit->setQueue($queueName,false,true,false,false,false,$arguments);
$rabbit->setExchange($exchangeName,AMQPExchangeType::FANOUT,false,true);
$rabbit->bindQueueExchange($queueName,$exchangeName);


//发送消息
$rabbit->sendMsg("TTL10s",[],$exchangeName);
$rabbit->waitForPendingAckReturns(5);

$rabbit->close();


